package com.toidep.fragment.mains;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.AdapterView.OnItemClickListener;

import com.toidep.R;
import com.toidep.activities.MainActivity;
import com.toidep.adapters.ModelAdapter;
import com.toidep.base.BaseFragment;
import com.toidep.config.ConfigUtility;
import com.toidep.config.Constants;
import com.toidep.widgets.sectionlist.AmazingListView;

public class ModelFragment extends BaseFragment implements OnClickListener{

	private String TAG = "ModelFragment";
	private final int TAB_NEW = 1;
	private final int TAB_HOT = 2;
	private final int TAB_TOP = 3;
	private final int TAB_ALL = 0;

	public boolean isRootFragment = true;
	
	private View view;

	private MainActivity mainActivity;
	
	private RelativeLayout buttonNew, buttonHot, buttonTop, buttonAll;
	private RelativeLayout layoutNew, layoutHot, layoutTop, layoutAll;
	
	private AmazingListView listModelsNew, listModelsHot, listModelsTop, listModelsAll;
	private ModelAdapter adapterModelsNew, adapterModelsHot, adapterModelsTop, adapterModelsAll;
	
	private boolean isRequestedNewData = false;
	private boolean isRequestedHotData = false;
	private boolean isRequestedTopData = false;
	private boolean isRequestedAllData = false;	

	/**
	 * Set lai cac base value cho fragment: cac item cua thanh header, currentCategory cua menuSideBar
	 */
	public void setBaseValues() {
		mainActivity.currentCategory = Constants.CATEGORY_MODEL;
		mainActivity.mainFragment.setTittle(mainActivity.getString(R.string.label_category_model));
		mainActivity.mainFragment.showButtonSlide(true);
		
		isRequestedNewData = false;
		isRequestedHotData = false;
		isRequestedTopData = false;
		isRequestedAllData = false;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		view = inflater.inflate(R.layout.fragment_model, container, false);

		mainActivity = (MainActivity) getActivity();
//
//		setTittle(getResources().getString(R.string.label_category_model));

		initComponents();

		setBaseValues();
		
		switchMenuModel(TAB_NEW);
		
		return view;
	}

	private void initComponents() {

		buttonNew = (RelativeLayout) view.findViewById(R.id.layoutButtonNewOfModels);
		buttonNew.setOnClickListener(this);
		buttonHot = (RelativeLayout) view.findViewById(R.id.layoutButtonHotOfModels);
		buttonHot.setOnClickListener(this);
		buttonTop = (RelativeLayout) view.findViewById(R.id.layoutButtonTopOfModels);
		buttonTop.setOnClickListener(this);
		buttonAll = (RelativeLayout) view.findViewById(R.id.layoutButtonAllOfModels);
		buttonAll.setOnClickListener(this);
		
		layoutNew = (RelativeLayout) view.findViewById(R.id.layoutModelNew);
		layoutHot = (RelativeLayout) view.findViewById(R.id.layoutModelHot);
		layoutTop = (RelativeLayout) view.findViewById(R.id.layoutModelTop);
		layoutAll = (RelativeLayout) view.findViewById(R.id.layoutModelAll);
	}
	
	public static ModelFragment newInstance() {
		ModelFragment myFragment = new ModelFragment();

		myFragment.setRootFragment(true);
		return myFragment;
	}
	
	public void switchMenuModel(int tabId) {
		
		switch (tabId) {
		case TAB_NEW:
			// Menu color
			buttonNew.setBackgroundResource(R.drawable.bg_red_normal);
			buttonHot.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonTop.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonAll.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			
			// Layout content
			layoutNew.setVisibility(View.VISIBLE);
			layoutHot.setVisibility(View.GONE);
			layoutTop.setVisibility(View.GONE);
			layoutAll.setVisibility(View.GONE);
			break;
			
		case TAB_HOT:
			// Menu color
			buttonNew.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonHot.setBackgroundResource(R.drawable.bg_red_normal);
			buttonTop.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonAll.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			
			// Layout content
			layoutNew.setVisibility(View.GONE);
			layoutHot.setVisibility(View.VISIBLE);
			layoutTop.setVisibility(View.GONE);
			layoutAll.setVisibility(View.GONE);
			break;
			
		case TAB_TOP:
			// Menu color
			buttonNew.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonHot.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonTop.setBackgroundResource(R.drawable.bg_red_normal);
			buttonAll.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			
			// Layout content
			layoutNew.setVisibility(View.GONE);
			layoutHot.setVisibility(View.GONE);
			layoutTop.setVisibility(View.VISIBLE);
			layoutAll.setVisibility(View.GONE);
			break;
			
		case TAB_ALL:
			// Menu color
			buttonNew.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonHot.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonTop.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonAll.setBackgroundResource(R.drawable.bg_red_normal);
			
			// Layout content
			layoutNew.setVisibility(View.GONE);
			layoutHot.setVisibility(View.GONE);
			layoutTop.setVisibility(View.GONE);
			layoutAll.setVisibility(View.VISIBLE);
			break;

		default:
			break;
		}
		
		requestData(tabId);
		setPaddingAllButton();
		
	}
	
	private void setPaddingAllButton() {
		int paddingDP = (int)(10 * ConfigUtility.scale);
		buttonNew.setPadding(paddingDP, paddingDP, paddingDP, paddingDP);
		buttonHot.setPadding(paddingDP, paddingDP, paddingDP, paddingDP);
		buttonTop.setPadding(paddingDP, paddingDP, paddingDP, paddingDP);
		buttonAll.setPadding(paddingDP, paddingDP, paddingDP, paddingDP);
	}

	/**
	 * Goi API get list models
	 */
	private void requestData(int tabId) {
		switch (tabId) {
		case TAB_NEW:
			if (isRequestedNewData)
				return;
			isRequestedNewData = true;
			
			// Khoi tao list model New
			listModelsNew = (AmazingListView) view.findViewById(R.id.listModelNew);
			listModelsNew.setAdapter(adapterModelsNew = new ModelAdapter(this, Constants.CATEGORY_MODEL, tabId));
			listModelsNew.setLoadingView(getActivity().getLayoutInflater().inflate(R.layout.layout_loading_view, null));
			adapterModelsNew.setAmazingListView(listModelsNew, ConfigUtility.PAGE_SIZE);
			adapterModelsNew.notifyMayHaveMorePages();
			
			// Chuyen toi detail cua model
			listModelsNew.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
					if (adapterModelsNew != null && position >= 0) {
						adapterModelsNew.onItemClick(position);
					}
				}
			});
			adapterModelsNew.resetList();
			break;
		case TAB_HOT:
			if (isRequestedHotData)
				return;
			isRequestedHotData = true;
			
			// Khoi tao list model Hot
			listModelsHot = (AmazingListView) view.findViewById(R.id.listModelHot);
			listModelsHot.setAdapter(adapterModelsHot = new ModelAdapter(this, Constants.CATEGORY_MODEL, tabId));
			listModelsHot.setLoadingView(getActivity().getLayoutInflater().inflate(R.layout.layout_loading_view, null));
			adapterModelsHot.setAmazingListView(listModelsHot, ConfigUtility.PAGE_SIZE);
			adapterModelsHot.notifyMayHaveMorePages();
			
			// Chuyen toi detail cua model
			listModelsHot.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
					if (adapterModelsHot != null && position >= 0) {
						adapterModelsHot.onItemClick(position);
					}
				}
			});
			adapterModelsHot.resetList();
			break;
		case TAB_TOP:
			if (isRequestedTopData)
				return;
			isRequestedTopData = true;
			
			// Khoi tao list model Top
			listModelsTop = (AmazingListView) view.findViewById(R.id.listModelTop);
			listModelsTop.setAdapter(adapterModelsTop = new ModelAdapter(this, Constants.CATEGORY_MODEL, tabId));
			listModelsTop.setLoadingView(getActivity().getLayoutInflater().inflate(R.layout.layout_loading_view, null));
			adapterModelsTop.setAmazingListView(listModelsTop, ConfigUtility.PAGE_SIZE);
			adapterModelsTop.notifyMayHaveMorePages();
			
			// Chuyen toi detail cua model
			listModelsTop.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
					if (adapterModelsTop != null && position >= 0) {
						adapterModelsTop.onItemClick(position);
					}
				}
			});
			adapterModelsTop.resetList();
			break;
		case TAB_ALL:
			if (isRequestedAllData)
				return;
			isRequestedAllData = true;
			
			// Khoi tao list model All
			listModelsAll = (AmazingListView) view.findViewById(R.id.listModelAll);
			listModelsAll.setAdapter(adapterModelsAll = new ModelAdapter(this, Constants.CATEGORY_MODEL, tabId));
			listModelsAll.setLoadingView(getActivity().getLayoutInflater().inflate(R.layout.layout_loading_view, null));
			adapterModelsAll.setAmazingListView(listModelsAll, ConfigUtility.PAGE_SIZE);
			adapterModelsAll.notifyMayHaveMorePages();
			
			// Chuyen toi detail cua model
			listModelsAll.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
					if (adapterModelsAll != null && position >= 0) {
						adapterModelsAll.onItemClick(position);
					}
				}
			});
			adapterModelsAll.resetList();
			break;
		}
	}
	
	@Override
	public void onClick(View v) {

		if (v == buttonNew) {
			switchMenuModel(TAB_NEW);
		} else if (v == buttonHot) {
			switchMenuModel(TAB_HOT);
		} else if (v == buttonTop) {
			switchMenuModel(TAB_TOP);
		} else if (v == buttonAll) {
			switchMenuModel(TAB_ALL);
		}
		
	}

}
